package com.mvp.shq.washer.zbar;

import android.content.Intent;
import android.os.Vibrator;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;

import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zbar.ZBarView;

/**
 * @author Administrator
 */
public class ZbarActivity extends AppCompatActivity implements QRCodeView.Delegate {

    private QRCodeView mQRCodeView;
    private TextView tvOpenLight;
    private boolean isOpen = false;
    private ImageView imgOpenLight;
    public static final String INTENT_EXTRA_KEY_QR_SCAN = "qr_scan_result";
    public static final int RESULT_CODE_QR_SCAN = 0xA1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zbar);
        StatusBarUtil.setColor(this, ContextCompat.getColor(getBaseContext(),R.color.shop_text_black),8);
        mQRCodeView = (ZBarView) findViewById(R.id.zbarview);
        mQRCodeView.setDelegate(this);
        ImageView back = (ImageView) findViewById(R.id.scanner_toolbar_back);
        back.setOnClickListener(v -> finish());
        LinearLayout openLight = (LinearLayout) findViewById(R.id.ll_open_light);
        tvOpenLight = (TextView)findViewById(R.id.tv_open_light);
        imgOpenLight = (ImageView)findViewById(R.id.img_open_light);
        openLight.setOnClickListener(v -> {
            //开灯
            if (!isOpen){
                tvOpenLight.setText("关闭手电筒");
                imgOpenLight.setImageResource(R.mipmap.flashlight_press);
                mQRCodeView.openFlashlight();
                isOpen = true;
            }else {//关灯
                tvOpenLight.setText("打开手电筒");
                imgOpenLight.setImageResource(R.mipmap.flashlight);
                mQRCodeView.closeFlashlight();
                isOpen = false;
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        mQRCodeView.startCamera();

        mQRCodeView.showScanRect();
        mQRCodeView.startSpot();
    }

    @Override
    protected void onStop() {
        mQRCodeView.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mQRCodeView.onDestroy();
        super.onDestroy();
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        vibrate();
        Intent resultIntent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString(INTENT_EXTRA_KEY_QR_SCAN, result);
        resultIntent.putExtras(bundle);
        this.setResult(RESULT_CODE_QR_SCAN, resultIntent);
        finish();
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        Toast.makeText(this, "打开照相机错误", Toast.LENGTH_SHORT).show();
    }
}
